import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'model/price_model.dart';
import 'webview_page.dart';
import 'function.dart';

class PricePage extends StatefulWidget {
  @override
  _PricePageState createState() => new _PricePageState();
}

class _PricePageState extends State<PricePage> {
  RefreshController _refreshController = new RefreshController();
  _PricePageState(){
    refresh(false);
  }
  refresh(bool direction) async{
    if(direction==false){
      _refreshController.sendBack(true, RefreshStatus.refreshing);
    }else{
      for(PriceModel price in platforms){
        price.getPrice().then((value){
          if(mounted){
            setState(() { });
          }
        });
      }
      
      new Duration(seconds: 1);
      _refreshController.sendBack(true, RefreshStatus.completed);

    }


  }
  Widget _buildPrice(PriceModel price) {
    return new Column(
      children: <Widget>[
        new ListTile(
          onTap: () async{
            print(price.invite);
            Navigator.of(context).push(
              new CupertinoPageRoute(builder: (context) {
                print(price.name);
                return new WebViewPage(price.invite,price.name);
              }),
            );
          },
          title: new Row(
            children: <Widget>[
              new Expanded(
                child: new Text(price.name,textAlign: TextAlign.center,),
              ),
              new Expanded(
                child: new Text(price.current??"最新价",textAlign: TextAlign.center,style: new TextStyle(fontSize: 12.0),),
              ),
              new Expanded(
                child: new Text(price.amount??"交易量",textAlign: TextAlign.center,style: new TextStyle(fontSize: 12.0),),
              ),
              new Expanded(
                child: new Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Text(
                      price.min??"最低",
                      style: new TextStyle(color: Colors.green,fontSize: 12.0),
                    ),
                    new Text("/"),
                    new Text(
                      price.max??"最高",
                      style: new TextStyle(color: Colors.red,fontSize: 12.0),
                    ),
                  ],
                ),
              ),
              new Expanded(
                child: new Text(
                  (price.change??"涨跌") + "%",
                  style: new TextStyle(color: price.upDown ? Colors.red : Colors.green,fontSize: 12.0),
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          ),
        ),
        Divider(height: 1.0),
      ],
    );
  }
  Widget _pulldownBuilder(context, mode) {
    return new ClassicIndicator(
      mode: mode,
      idleText: "继续下拉刷新",
      completeText: "刷新完成",
      refreshingText: "刷新中…",
      releaseText: "松手开始刷新",
      refreshingIcon: const CircularProgressIndicator(
        strokeWidth: 3.0,
      ),
      completeIcon: const Icon(Icons.done, size: 32.0, color: Colors.green),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData(
        accentColor: Colors.deepOrange,
        primaryColor: Colors.deepOrange,
      ),
      home: new Scaffold(
        appBar: new CupertinoNavigationBar(
          actionsForegroundColor: Colors.white,
          backgroundColor: Colors.deepOrange,
          middle: new Text(
            "币价行情",
            style: new TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.normal,
            ),
          ),
        ),
        body: new SmartRefresher(
          enablePullDown: true,
          onRefresh: refresh,
          headerBuilder: _pulldownBuilder,
          controller: _refreshController,
          child: new ListView.builder(
            itemCount: platforms.length,
            itemBuilder: (context, i) => _buildPrice(platforms[i]),
          ),
        ),
//          child:
      ),
    );
  }
}
